#include"mouse.h"

Mouse* mkMouse(int startx, int starty, int height, int wide){
	Mouse* mickey = (Mouse*)malloc(sizeof(Mouse));
	mickey->x = startx;
	mickey->y = starty;
	mickey->height = height;
	mickey->wide = wide;
	return mickey;
}

void move(Mouse* mickey, int* puzzle, int way){
	gotoxy(mickey->x+1,mickey->y+1);
	printf("*");	
	switch(way){
		case 2: // go east
			puzzle[(++mickey->x)+(mickey->y)*(mickey->wide)]=1;
			break;
		case 3: //go south
			puzzle[(mickey->x)+(++mickey->y)*(mickey->wide)]=1;
			break;
		case 0: //go west
			puzzle[(--mickey->x)+(mickey->y)*(mickey->wide)]=1;
			break;
		case 1: //go north
			puzzle[(mickey->x)+(--mickey->y)*(mickey->wide)]=1;
			break;
	}
	gotoxy(mickey->x+1,mickey->y+1);
	printf("M");	
}


int findway(Mouse* mickey, int* puzzle, int* way){
	int direct = ((*way)+1)%4;
	int hold=0;
	int count=0;
//	printf("way= %d, direct=%d",*way,direct);
	while(direct !=(* way)){	
		switch(direct){
		case 0: // go east
			if((mickey->x)+1 >= 80){
				return 7;
			}
			if(puzzle[(mickey->x+1)+(mickey->y)*(mickey->wide)] == 0){
               // printf("%d",puzzle[(mickey->x+1)+(mickey->y)*(mickey->wide)]);
				count++;
				hold=(direct+2)%4;
			}			
			break;
		case 1: //go south
			if((mickey->y)+1 >= 25){
				return 7;
			}
			if(puzzle[(mickey->x)+(mickey->y+1)*(mickey->wide)] == 0){
               // printf("%d",puzzle[(mickey->x)+(mickey->y+1)*(mickey->wide)]);
				count++;
				hold=(direct+2)%4;
			}
			break;
		case 2: //go west
			if((mickey->x)-1 < 0){
				return 7;
			}
			if(puzzle[(mickey->x-1)+(mickey->y)*(mickey->wide)] == 0){
            //    printf("%d",puzzle[(mickey->x-1)+(mickey->y)*(mickey->wide)]);
				count++;
				hold=(direct+2)%4;
			}
			break;
		case 3: //go north
			if((mickey->y)-1 < 0){
				return 7;
			}
			if(puzzle[(mickey->x)+(mickey->y-1)*(mickey->wide)] == 0){
              //  printf("%d",puzzle[(mickey->x)+(mickey->y-1)*(mickey->wide)]);
				count++;
				hold=(direct+2)%4;
			}
			break;
		}
		direct = (direct+1)%4;
	}
	printf("count = %d ", count);
	(*way)=hold;
	return count;
}

void killMouse(Mouse* mickey){
	free(mickey);
}
